/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package 解析.node.node;

import 解析.Matcher;

/**
 *
 * @author yanxi
 */
public class NotBehind extends Node { 
    public Node cond;
    public int rmax, rmin;

    public NotBehind(Node cond, int rmax, int rmin) {
        this.cond = cond;
        this.rmax = rmax;
        this.rmin = rmin;
        getChildren().add(cond);  
    }
    
    @Override
    public boolean match(Matcher matcher, int i, CharSequence seq) {
        int savedLBT = matcher.lookbehindTo;
        int savedFrom = matcher.from;
        boolean conditionMatched = false;
        int startIndex = (!matcher.transparentBounds)
                ? matcher.from : 0;
        int from = Math.max(i - rmax, startIndex);
        matcher.lookbehindTo = i;
        // Relax transparent region boundaries for lookbehind
        if (matcher.transparentBounds) {
            matcher.from = 0;
        }
        for (int j = i - rmin; !conditionMatched && j >= from; j--) {
            conditionMatched = cond.match(matcher, j, seq);
        }
        // Reinstate region boundaries
        matcher.from = savedFrom;
        matcher.lookbehindTo = savedLBT;
        return !conditionMatched && next.match(matcher, i, seq);
    } 
    
    @Override
    public String toString(){
        //return "["+this.getClass().getName()+"]"+"rmin:"+rmin+",rmax:"+rmax;
        return cond.toString(); 
    }
}
